import * as bcryptjs from 'bcryptjs';
// 加密明文
export function encryptText(
  plainText: string,
  saltRounds: number = 10,
): string | null {
  // 使用 saltRounds 作为成本因子来散列密码
  // 更高的 saltRounds 值意味着计算将更慢，但更安全
  let res: string | null = null;
  if (!plainText) return null;
  try {
    res = bcryptjs.hashSync(plainText, saltRounds);
  } catch (error) {
    res = null;
  }
  return res;
}

// 验证
// plainText明文，hashedStr加密后的哈希字符串
export function verifyText(plainText: string, hashedStr: string): boolean {
  let compare: boolean = false;
  if (!plainText || !hashedStr) return compare;
  try {
    compare = bcryptjs.compareSync(plainText, hashedStr);
  } catch (error) {
    compare = false;
  }
  return compare;
}
